package com.dy.security.filter;

import org.springframework.http.HttpMethod;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: ding-yu
 * @Date: 2022/5/7 14:03
 * @Desctiption: (描述) 自定义过滤器
 */
public class MySecurityFilter extends OncePerRequestFilter {
    private static final String LOGIN_RUL="/login";
    //继承OncePerRequestFilter来实现，可以确保一次请求只会通过一次该过滤器
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //登录请求
        if (LOGIN_RUL.equals(request.getRequestURI()) && request.getMethod().equals(HttpMethod.POST.name())) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.err.println("username+password  "+username +"  "+ password);
        }else{
            //非登录请求
            System.err.println("非登录请求");
        }
        filterChain.doFilter(request,response);
    }
}
